package pos.utils;

/**
 * 平面对象，系统中2D模拟的基础
 * @author plus
 */
import javax.vecmath.*;
import javax.media.j3d.*;

public class Plane2D extends Shape3D {

    private float width,  height ,depth;
    private TriangleStripArray Gem;
    private Appearance App;
    private ColoringAttributes AppCA = new ColoringAttributes();
    private TransparencyAttributes AppTA = new TransparencyAttributes();
    private TextureAttributes AppTextureA = new TextureAttributes();
    private RenderingAttributes AppRenderA = new RenderingAttributes();

    public Plane2D(float w, float h, float depth,Color3f color, float alpha,
            Texture texture, boolean bNoAlpha,boolean bPickable) {
        width = w;
        height = h;
        this.depth=depth;
        Gem = new TriangleStripArray(4, TriangleStripArray.COORDINATES |
                TriangleStripArray.TEXTURE_COORDINATE_2, new int[]{4});		//��������
        Gem.setCoordinates(0, getVectexes());											//���붥�����
        Gem.setTextureCoordinates(0, 0, getTexCoord());									//�����������

        App = new Appearance();

        AppTA.setCapability(AppTA.ALLOW_VALUE_WRITE);
        //AppTA.setCapability(AppTA.ALLOW_MODE_WRITE);
        AppTA.setTransparency(alpha);
        AppTA.setSrcBlendFunction(TransparencyAttributes.BLEND_SRC_ALPHA);
        AppTA.setDstBlendFunction(TransparencyAttributes.BLEND_ONE_MINUS_SRC_ALPHA);
        AppTA.setTransparencyMode(TransparencyAttributes.BLENDED);
        
        App.setTransparencyAttributes(AppTA);

        if (texture != null) {
            AppTextureA.setTextureMode(TextureAttributes.MODULATE);
            App.setTextureAttributes(AppTextureA);
            App.setTexture(texture);
        }

       if (color != null) {
            AppCA.setCapability(ColoringAttributes.ALLOW_COLOR_WRITE);
            AppCA.setColor(color);
            App.setColoringAttributes(AppCA);
        }

        App.setPolygonAttributes(
                new PolygonAttributes(PolygonAttributes.POLYGON_FILL, 
                PolygonAttributes.CULL_BACK, 0f));

        if (bNoAlpha) {
            AppTA.setTransparencyMode(TransparencyAttributes.NONE);
        }

            AppRenderA.setDepthTestFunction(RenderingAttributes.ALWAYS);
            App.setRenderingAttributes(AppRenderA);
            
        App.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
        this.setCapability(ALLOW_PICKABLE_WRITE);
        
        this.setPickable(bPickable);
        this.setGeometry(Gem);
        this.setAppearance(App);
    }

    public Plane2D(float w, float h, float depth,Color3f color, float alpha,
            Texture texture, boolean bNoAlpha,boolean bPickable, boolean bTop) {
        width = w;
        height = h;
        this.depth=depth;
        Gem = new TriangleStripArray(4, TriangleStripArray.COORDINATES |
                TriangleStripArray.TEXTURE_COORDINATE_2, new int[]{4});		//��������
        Gem.setCoordinates(0, getVectexes());											//���붥�����
        Gem.setTextureCoordinates(0, 0, getTexCoord());									//�����������

        App = new Appearance();

        AppTA.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
        //AppTA.setCapability(AppTA.ALLOW_MODE_WRITE);
        AppTA.setTransparency(alpha);
        AppTA.setSrcBlendFunction(TransparencyAttributes.BLEND_SRC_ALPHA);
        AppTA.setDstBlendFunction(TransparencyAttributes.BLEND_ONE_MINUS_SRC_ALPHA);
        AppTA.setTransparencyMode(TransparencyAttributes.BLENDED);

        App.setTransparencyAttributes(AppTA);

        if (texture != null) {
            AppTextureA.setTextureMode(TextureAttributes.MODULATE);
            App.setTextureAttributes(AppTextureA);
            App.setTexture(texture);
        }

       if (color != null) {
            AppCA.setCapability(ColoringAttributes.ALLOW_COLOR_WRITE);
            AppCA.setColor(color);
            App.setColoringAttributes(AppCA);
        }

        App.setPolygonAttributes(
                new PolygonAttributes(PolygonAttributes.POLYGON_FILL,
                PolygonAttributes.CULL_BACK, 0f));

        if (bNoAlpha) {
            AppTA.setTransparencyMode(TransparencyAttributes.NONE);
        }

        if (bTop){
            AppRenderA.setDepthTestFunction(RenderingAttributes.ALWAYS);
            App.setRenderingAttributes(AppRenderA);
        }

        this.setPickable(bPickable);
        this.setGeometry(Gem);
        this.setAppearance(App);
    }
    public void setColor(Color3f color) {
        //ColoringAttributes AppCA=new ColoringAttributes();
        AppCA.setColor(color);
    //App.setColoringAttributes(AppCA);
    }

    public void setAlpha(float alpha) {
        //TransparencyAttributes AppTA = new TransparencyAttributes();
        AppTA.setTransparency(alpha);
//        AppTA.setTransparencyMode(TransparencyAttributes.NICEST);
        //App.setTransparencyAttributes(AppTA);
    }

    public void setTexture(Texture texture) {
        if (texture != null) {
            App.setTexture(texture);
        }
    }
    
    private Point3f[] getVectexes() {
        Point3f[] res = {
            new Point3f((float) (-.5 * width), (float) (.5 * height), depth),
            new Point3f((float) (-.5 * width), (float) (-.5 * height), depth),
            new Point3f((float) (.5 * width), (float) (.5 * height), depth),
            new Point3f((float) (.5 * width), (float) (-.5 * height), depth),
        };
        return res;
    }

    private float[] getTexCoord() {
        float[] res = {
            0f, 1f,
            0f, 0f,
            1f, 1f,
            1f, 0f
        };
        return res;
    }
}
